// components/custom-address/index.js
import { request } from '../../utils/request'
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        showPicker: {
            type: Boolean,
            value:　false
        },
        currentAddress: {
            type: Array,
            value:　[{}]
        },
        currentIndex: {
            type: Number,
            value:　0
        },
        firstLevel: {
            type: Number,
            value:　1
        },
        addressList: {
            type: Array,
            value:　[]
        },
        title: {
            type: String,
            value:　'请选择所在地区'
        },
        level: {
            type: Number,
            value: 5
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        // currentAddress: [{}],
        // currentIndex: 0,
        // addressList: [],
        keys: ['proCode', 'cityCode', 'areaCode', 'townCode', 'villagCode'],
        isLoading: false
    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () { 
            
        },
        ready: function () {
            //this.getProvinceData()
            // console.log(this.data.defaultAddress)
        },
        moved: function () { 
            //console.log("moved")
        },
        detached: function () { 
            //console.log("detached")
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // getProvinceData() {
        //     let that = this;
        //     request({
        //         url: "/apiAreaController/getProvinceData"
        //     }).then(res => {
        //         if(res.code == 0){
        //             that.setData({
        //                 addressList: [res.data]
        //             })
        //             console.log(this.data.defaultAddress)
        //         }
        //     })
        // },
        getAreaListByCode(e) {
            if(this.data.isLoading){
                return
            }
            this.setData({
                isLoading: true
            })
            let that =  this;
            let { item, index } = e.currentTarget.dataset;
            let { currentAddress, currentIndex, addressList, level, firstLevel } = that.data;
            // if(item.plevel < level){
            //     currentAddress.splice(currentIndex,1,{})
            //     currentAddress.splice(currentIndex,0,item)
            //     currentIndex = item.plevel;
            // }
            level = level + firstLevel - 1;
            if(item.pLevel == level){
                currentAddress.splice(currentIndex,1,item)
                that.setData({
                    currentAddress: currentAddress,
                    // currentIndex: currentIndex,
                    isLoading: false
                })
                
                this.triggerEvent('getAddr', { showPicker: false, address: this.getkeyValue() });
                return
            }
            request({
                url: "/commonApi/getAdministrativeDivision",
                method: "GET",
                // showLoading: false,
                data: {
                    proCode: item.code
                }
            }).then(res => {
                if(res.code == 0){
                    if(!res.data){
                        currentAddress.splice(currentIndex,1,item)
                        that.setData({
                            currentAddress: currentAddress,
                        })
                        that.getkeyValue()
                        that.triggerEvent('getAddr', { showPicker: false, address: that.getkeyValue() });
                        return
                    }
                    if(item.pLevel < level){
                        currentAddress.splice(currentIndex,1,{})
                        currentAddress.splice(currentIndex,0,item)
                        currentIndex = item.pLevel - firstLevel + 1;
                    }
                    that.setData({
                        currentAddress: currentAddress,
                        currentIndex: currentIndex,
                        addressList: [...addressList,res.data]
                    })
                }
                this.setData({
                    isLoading: false
                })
            }).catch(error => {
                this.setData({
                    isLoading: false
                })
                // console.log(error)
            })
        },
        toggleItem(e){
            let { index } = e.currentTarget.dataset;
            let { currentAddress, addressList } = this.data;
            let current = [], list = [];
            currentAddress.forEach((item,i) => {
                if(index>=i){
                    current.push(item)
                }
            })
            addressList.forEach((item,i) => {
                if(index>=i){
                    list.push(item)
                }
            })
            this.setData({
                currentAddress: current,
                currentIndex: index,
                addressList: list
            })
            
        },
        closeAddr() {
            this.triggerEvent('closeAddr', { showPicker: false });
        },
        getkeyValue(){
            let keys = this.data.keys.slice(this.data.firstLevel-1);
            let address = [];
            keys.forEach((item,index) => {
                address.push({
                    key: item,
                    value: this.data.currentAddress[index]
                })
            })
            return address
        }
    }
})